package com.lp.enums;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lp.canal.EntryHandler;
import com.lp.handler.UserHandler;
import lombok.Getter;

import java.util.Objects;

/**
 * 消息枚举
 *
 * @author 10263
 */
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum CanalFactoryEnum {

    /**
     * Canal工厂
     */
    JYN_REDEEM_INTEGRAL_RECORD("test.user", UserHandler.class);

    private final String code;
    private final Class clazz;

    <T extends EntryHandler> CanalFactoryEnum(String code, Class<T> clazz) {
        this.code = code;
        this.clazz = clazz;
    }

    /**
     * 根据CODE获取对象
     *
     * @param code
     * @return
     */
    public static <T extends EntryHandler> Class<T> getClazz(String code) {
        for (CanalFactoryEnum classEnum : CanalFactoryEnum.values()) {
            if (Objects.equals(code, classEnum.getCode())) {
                return classEnum.getClazz();
            }
        }
        return null;
    }

    /**
     * 获取所有表
     *
     * @return
     */
    public static String getTables() {
        StringBuilder buffer = new StringBuilder();
        for (CanalFactoryEnum classEnum : CanalFactoryEnum.values()) {
            buffer.append(classEnum.getCode()).append(",");
        }
        return buffer.deleteCharAt(buffer.length() - 1).toString();
    }
}
